package com.dsp.services.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import com.core.util.DspUtil;
import com.core.util.GenericDAO;
import com.core.util.GenericServiceHibernate;
import com.dsp.bean.Menu;
import com.dsp.bean.MenuTree;
import com.dsp.dao.MenuDAO;
import com.dsp.services.MenuService;

@Service("menuService")
public class MenuServiceImpl extends GenericServiceHibernate<Menu, Long> implements MenuService {
	
	Logger logger = Logger.getLogger(DspUtil.Log4J);
	
	private MenuDAO menuDAO ;
	
	@Resource(name = "menuDAO")
	public void setDao(GenericDAO<Menu, Serializable> dao) {
		super.setDao(dao);
	}

	@Override
	public List<MenuTree> getMenus() {
		List<MenuTree> menuTree = new ArrayList<MenuTree>();
		DetachedCriteria dc = DetachedCriteria.forClass(Menu.class);
		DetachedCriteria subDC = DetachedCriteria.forClass(Menu.class);
		dc.add(Restrictions.eq("parent", "0"));
		List<Menu> list = super.likeBy(dc);
		List<Menu> subList = super.likeBy(subDC);
		Menu m , s;
		MenuTree mt = null;
		for(int i = 0 ; i < list.size() ; i++){
			m = list.get(i);
			mt = new MenuTree(m.getId(),m.getIcon() ,m.getTag(),null);
			for(int j = 0 ; j < subList.size() ; j++) {
				s = subList.get(j);
				if(m.getId().toString().equals(s.getParent())){
					mt.add(new MenuTree(s.getId(),s.getIcon(),s.getTag(),s.getEvent()));
				}
			}
			menuTree.add(mt);
		}
		return menuTree;
	}

	/**
	 * @return the menuDAO
	 */
	public MenuDAO getMenuDAO() {
		return menuDAO;
	}

	/**
	 * @param menuDAO the menuDAO to set
	 */
	public void setMenuDAO(MenuDAO menuDAO) {
		this.menuDAO = menuDAO;
	}
	
	
}
